#include <iostream>
#include <string>
#include "dir.h"
#include <archive.h>
#include <archive_entry.h>
#include "fs-archive.h"

using namespace std;

void CArch::refresh() {
  cerr << "-> CArch::refresh" << endl;
  archive *a = archive_read_new();
  archive_read_support_format_all(a);
  archive_read_support_compression_all(a);

  int r = archive_read_open_file(a, Name().c_str(), 10240);
  if (r) {
    //cerr << "не удалось открыть архив " << argv[1] << endl;
    //cerr << "ошибка " << archive_error_string(a) << endl;
    //return -1;
    throw;
  }

  int i = 0;

  while (1) {
    archive_entry *header = NULL;
    r = archive_read_next_header(a, &header);
    if (ARCHIVE_EOF == r) break;
    if (ARCHIVE_OK != r) {
      //cerr << "Ошибка: " << archive_error_string(a) << endl;
      if (r < ARCHIVE_WARN) 
        throw;
        //return -1;
    }
    string s_new_name = Name() + " / " + archive_entry_pathname(header);
    IEntry *pe = new CArch::CArchTextFile(s_new_name,
                                          new CArch::CBinFile(Name() + " / " + archive_entry_pathname(header), a));
                 //new CFile(Name() + " / " + archive_entry_pathname(header));
    //filesystem.Create(Name() + "/" + archive_entry_pathname(header));
    ve.push_back(pe);

    i++;
    if (i > 100) break;
    cerr << ".";
 
    //cout << "file " << archive_entry_pathname(header) << endl;
    
  }
  archive_read_close(a); 
  cerr << "<- CArch::refresh()" << endl;
}


